package com.bw.service;

import com.bw.domain.Message;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.annotation.Retryable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SendService {
    @Autowired
    RocketMQTemplate rocketMQTemplate;
    @Retryable
    @Async
    public  void send(Message message){
        SendResult sendResult = rocketMQTemplate.syncSend("Mymesage", message);
        SendStatus sendStatus = sendResult.getSendStatus();
        if (sendStatus.equals(SendStatus.SEND_OK)){
            log.info("消息发送成功{}",message);
        }else{
            log.info("消息发送失败{}",message);
        }
    }
    @Retryable
    @Async
    public  void asend(Message message){
  rocketMQTemplate.asyncSend("Mymesage", message, new SendCallback() {
      @Override
      public void onSuccess(SendResult sendResult) {
          log.info("消息发送成功{}",message);
      }

      @Override
      public void onException(Throwable throwable) {
          log.info("消息发送失败{}",message);
      }
  });

    }
}
